package cn.melon.view.admin.library;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.melon.mod.library.LibraryBooksInfo;
import cn.melon.view.admin.library.ctr.LIbraryBookInfoCtr;

public class BookInfoDialog extends JDialog {
    /**
     * 
     */
    private ActionListener listener;
    
    private static final long serialVersionUID = 1L;
    private JTextField bookID = new JTextField("",20);
    private JTextField bookName = new JTextField("",20);
    private JTextField publisher = new JTextField("",20);
    private JTextField author = new JTextField("",20);
    private JTextField loanable = new JTextField("",20);
    private JTextField borrowStudent = new JTextField("",20);
    private JTextField borrowDate = new JTextField("",20);
    private JTextField DDL = new JTextField("",20);

    
    private JButton saveButton = new JButton("保存");
    private JButton cancelButton = new JButton("取消");
    String[] textStr = {"书籍编号：","书籍名称：","出版社：","作者：","可借数量：","借阅人学号：","借出日期：","应还日期："};
    
    JTextField[] jTextFields = {bookID,bookName,publisher,author,loanable,borrowStudent,borrowDate,DDL};
    public BookInfoDialog(LibraryBooksInfo book,IUpdateBookTable updateTable) {
        //this.setUpdateBookTable(updateTable);
        listener = new LIbraryBookInfoCtr(this, updateTable);
        initialize();
        bookID.setText(book.getBookID());
        bookName.setText(book.getBookName());
        publisher.setText(book.getPublisher());
        author.setText(book.getAuthor());
        loanable.setText(book.getLoanable());
        borrowStudent.setText(book.getBorrowStudent());
        borrowDate.setText(book.getBorrowDate());
        DDL.setText(book.getDDL());
    }
    private void initialize(){
        this.setTitle("书籍信息");
        this.setSize(400, 300);
       
        JPanel panel = new JPanel();
        
        GridBagLayout layout = new GridBagLayout();
        panel.setLayout(layout);        

        for(int i=0;i<textStr.length;i++){
            GridBagConstraints constraints = new GridBagConstraints();
            constraints.anchor = GridBagConstraints.EAST;
            constraints.fill = GridBagConstraints.NONE;
            constraints.gridx = 0;
            constraints.gridy = i;
            constraints.weightx = 100;
            constraints.weighty = 100;
            panel.add(new JLabel(textStr[i]),constraints);
            constraints.anchor = GridBagConstraints.CENTER;
            constraints.gridx = 1;
            panel.add(jTextFields[i],constraints);
            constraints.anchor = GridBagConstraints.EAST;
            constraints.gridx = 2;
            panel.add(Box.createRigidArea(new Dimension(2, 15)));
        }
        
        JPanel tPanel = new JPanel();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 1;
        constraints.gridy = textStr.length;
        constraints.weightx = 100;
        constraints.weighty = 100;
        tPanel.add(saveButton);     //按钮
        tPanel.add(Box.createRigidArea(new Dimension(10, 15)));
        tPanel.add(cancelButton);
        panel.add(tPanel,constraints);      //按钮
        
        this.add(panel);
        this.setResizable(false);
        
        saveButton.addActionListener(listener);
        cancelButton.addActionListener(listener);
    }
    
    public LibraryBooksInfo getBookInfo(){
        LibraryBooksInfo booksInfo = new LibraryBooksInfo(bookName.getText(),bookID.getText(),publisher.getText(),author.getText(),loanable.getText(),borrowStudent.getText(),borrowDate.getText(),DDL.getText(),"");
        return booksInfo;
    }
    
    public static void main(String[] args) {
//        Dialog dialog = new BookInfoDialog();
//        dialog.setVisible(true);
    }
}
